package cn.itcast.bos.service.base.Impl;

import cn.itcast.bos.Dao.base.CourierDao;
import cn.itcast.bos.Dao.base.FixedAreaDao;
import cn.itcast.bos.Dao.base.TakeTimeDao;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.crm.domain.Customer;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.ws.rs.core.MediaType;
import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator on 2017/7/23.
 */
@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService {
    @Autowired
    private FixedAreaDao fixedAreaDao;

    @Override
    public void save(FixedArea model) {
        fixedAreaDao.save(model);
    }

    @Override
    public Page<FixedArea> pageQuery(Specification<FixedArea> specification, Pageable pageable) {
        return fixedAreaDao.findAll( specification,pageable);
    }

    @Override
    //查询没有定区Id的客户
    public List<Customer> findNoassociationCustomer() {
        Collection<? extends Customer> collection = WebClient.create("http://localhost:8081/crm_management/service/customerService/noAssociationCustomer").
                accept(MediaType.APPLICATION_JSON_TYPE).getCollection(Customer.class);
        return (List<Customer>) collection;
    }



    @Override
    //查询有定区id的客户
    public List<Customer> findassociationCustomer(String id) {
        Collection<? extends Customer> collection =
                WebClient.create("http://localhost:8081/crm_management/service/customerService/hasAssociationFixedAreaCustomers/"+id).
                type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON_TYPE).getCollection(Customer.class);
        return (List<Customer>) collection;
    }

    @Override
    //关联定区与客户的方法
    public void associationCustomersToFixedArea(String[] customerIds, String fixedAreaId) {
        //将字符数组按","组装成String
        String customerIdstr = StringUtils.join(customerIds,",");
       WebClient.create("http://localhost:8081/crm_management/service/customerService/associationCustomersToFixedArea?" +
               "customerId="+customerIdstr+"&fixedAreaId="+fixedAreaId).type(MediaType.APPLICATION_JSON_TYPE).
               accept(MediaType.APPLICATION_JSON_TYPE).put(null);
    }


    //注入快递员,与取派时间的到层接口
    @Autowired
    private CourierDao courierDao;
    @Autowired
    private TakeTimeDao takeTimeDao;
    @Override
    //关联定区与快递员的方法
    public void associationCourierToFixedArea( FixedArea model,Integer courierId, Integer takeTimeId) {
        //先查询到对应得快递员与取派时间,然后再设置定区
        FixedArea fixedArea = fixedAreaDao.findOne(model.getId());
        Courier courier = courierDao.findOne(courierId);
        TakeTime takeTime=takeTimeDao.findOne(takeTimeId);
        //将快递员绑定到定区
        fixedArea.getCouriers().add(courier);
        //收派时间关联到快递员上
        courier.setTakeTime(takeTime);

    }






}
